package com.api.league.controller;

import com.api.core.annotation.PowerEnable;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.league.form.HighlightForm;
import com.api.league.model.LeagueHighlight;
import com.api.league.service.LeagueHighlightService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author RenQiang
 * @date 2021/6/2
 */
@PowerEnable(name = "精彩瞬间", url = "/league/highlight")
@Api(value = "精彩瞬间", tags = {LeagueHighlightController.API_TAG})
@RestController
@RequestMapping("/league/highlight")
public class LeagueHighlightController {
    public static final String API_TAG = "精彩瞬间";

    private final LeagueHighlightService leagueHighlightService;

    public LeagueHighlightController(LeagueHighlightService leagueHighlightService) {
        this.leagueHighlightService = leagueHighlightService;
    }

    @ApiOperation(value = "保存精彩瞬间", tags = {API_TAG}, notes = "保存精彩瞬间")
    @PostMapping(value = "/save")
    public Result saveHighlight(@Valid HighlightForm form) {
        LeagueHighlight leagueHighlight = new LeagueHighlight();
        leagueHighlight.setLeagueId(form.getLeagueId());
        leagueHighlight.setId(form.getId());
        leagueHighlight.setTitle(form.getTitle());
        leagueHighlight.setIntroduction(form.getIntroduction());
        leagueHighlight.setVideoUrl(form.getVideoUrl());
        if (form.getId() == null) {
            leagueHighlightService.save(leagueHighlight);
        } else {
            leagueHighlightService.update(leagueHighlight);
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "分页查询精彩瞬间", tags = {API_TAG}, notes = "分页查询精彩瞬间")
    @GetMapping("/find-page")
    public Result findByPage(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                             @ApiParam(value = "页号") @RequestParam(defaultValue = "1") Integer page,
                             @ApiParam(value = "分页大小") @RequestParam(defaultValue = "10") Integer size) {
        PageInfo<LeagueHighlight> pageInfo = leagueHighlightService.findByPage(leagueId, page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "删除竞猜瞬间", tags = {API_TAG}, notes = "删除竞猜瞬间")
    @PostMapping("/delete")
    public Result deleteHighlight(@ApiParam(value = "竞猜瞬间ID") @RequestParam Long id) {
        leagueHighlightService.deleteById(id);
        return ResultGenerator.genDeleteSuccessResult();
    }
}
